package com.example.springinitialization.controller;

import com.example.springinitialization.annotation.AuthCheck;
import com.example.springinitialization.pojo.dto.userDto.*;
import com.example.springinitialization.pojo.response.user.UserResponse;
import com.example.springinitialization.response.Result;
import com.example.springinitialization.service.UserService;
import com.example.springinitialization.utils.DeleteDto;
import com.example.springinitialization.utils.token.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "01. 用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @AuthCheck(auth = "admin")
    @GetMapping("/list")
    @ApiOperation(value = "获取所有用户集合")
    public Result<List<UserResponse>> getUserList() {
        return Result.success(userService.getUserList());

    }

    @AuthCheck(auth = "admin")
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据参数获取所有用户集合")
    public Result<UserResponse> getUserById(@PathVariable Long id) {
        return Result.success(userService.getUserById(id));
    }

    @AuthCheck(auth = "admin")
    @PostMapping("/get/params")
    @ApiOperation(value = "根据id获取用户")
    public Result<List<UserResponse>> getUserById(@RequestBody UserQueryDto userQueryDto) {
        return Result.success(userService.getUserListByParams(userQueryDto));
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result<Boolean> register(@RequestBody UserResisterDto userResisterDto) {
        return Result.success(userService.resister(userResisterDto));
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<LoginUser> login(@RequestBody UserLoginDto userLoginDto) {
        return Result.success(userService.login(userLoginDto));
    }

    @PostMapping("/logout")
    @ApiOperation(value = "用户退出登录")
    public Result<Boolean> logout() {
        return Result.success(userService.logout());
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加用户")
    public Result<Boolean> addUser(@RequestBody UserAddDto userAddDto) {
        userService.AddUser(userAddDto);
        return Result.success(true);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新用户信息")
    public Result<Boolean> updateUser(@RequestBody UserUpdateDto userUpdateDto) {
        userService.UpdateUser(userUpdateDto);
        return Result.success(true);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户信息")
    public Result<Boolean> deleteUser(@RequestBody DeleteDto deleteDto) {
        userService.delUser(deleteDto);
        return Result.success(true);
    }
}
